<!DOCTYPE html>












  


<html class="theme-next pisces use-motion" lang="en">
<head><meta name="generator" content="Hexo 3.9.0">
  <meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">






















<link rel="stylesheet" href="/lib/font-awesome/css/font-awesome.min.css?v=4.7.0">

<link rel="stylesheet" href="/css/main.css?v=7.2.0">


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=7.2.0">


  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png?v=7.2.0">


  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png?v=7.2.0">


  <link rel="mask-icon" href="/images/logo.svg?v=7.2.0" color="#222">







<script id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Pisces',
    version: '7.2.0',
    sidebar: {"position":"left","display":"post","offset":12,"onmobile":false,"dimmer":false},
    back2top: true,
    back2top_sidebar: false,
    fancybox: false,
    fastclick: false,
    lazyload: false,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>


  




  <meta name="description" content="Linux之Shell脚本基础">
<meta name="keywords" content="Shell脚本">
<meta property="og:type" content="article">
<meta property="og:title" content="Linux之Shell脚本基础">
<meta property="og:url" content="http://yoursite.com/2021/02/23/Linux相关/Linux之Shell脚本基础/index.html">
<meta property="og:site_name" content="MingRong&#39;s Boat">
<meta property="og:description" content="Linux之Shell脚本基础">
<meta property="og:locale" content="en">
<meta property="og:updated_time" content="2021-02-23T07:03:01.475Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="Linux之Shell脚本基础">
<meta name="twitter:description" content="Linux之Shell脚本基础">





  
  
  <link rel="canonical" href="http://yoursite.com/2021/02/23/Linux相关/Linux之Shell脚本基础/">



<script id="page.configurations">
  CONFIG.page = {
    sidebar: "",
  };
</script>

  <title>Linux之Shell脚本基础 | MingRong's Boat</title>
  












  <noscript>
  <style>
  .use-motion .motion-element,
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-title { opacity: initial; }

  .use-motion .logo,
  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="en">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta">
    

    <div class="custom-logo-site-title">
      <a href="/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">MingRong's Boat</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
    
      
        <p class="site-subtitle">O Captain! My Captain!</p>
      
    
    
  </div>

  <div class="site-nav-toggle">
    <button aria-label="Toggle navigation bar">
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>



<nav class="site-nav">
  
    <ul id="menu" class="menu">
      
        
        
        
          
          <li class="menu-item menu-item-home">

    
    
      
    

    

    <a href="/" rel="section"><i class="menu-item-icon fa fa-fw fa-home"></i> <br>Home</a>

  </li>
        
        
        
          
          <li class="menu-item menu-item-categories">

    
    
      
    

    

    <a href="/categories/" rel="section"><i class="menu-item-icon fa fa-fw fa-th"></i> <br>Categories</a>

  </li>
        
        
        
          
          <li class="menu-item menu-item-tags">

    
    
      
    

    

    <a href="/tags/" rel="section"><i class="menu-item-icon fa fa-fw fa-tags"></i> <br>Tags</a>

  </li>
        
        
        
          
          <li class="menu-item menu-item-archives">

    
    
      
    

    

    <a href="/archives/" rel="section"><i class="menu-item-icon fa fa-fw fa-archive"></i> <br>Archives</a>

  </li>
        
        
        
          
          <li class="menu-item menu-item-about">

    
    
      
    

    

    <a href="/about/" rel="section"><i class="menu-item-icon fa fa-fw fa-user"></i> <br>About</a>

  </li>

      
      
    </ul>
  

  

  
</nav>



  



</div>
    </header>

    


    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          
            

          
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2021/02/23/Linux相关/Linux之Shell脚本基础/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="MingRongChen">
      <meta itemprop="description" content="O Captain! My Captain!">
      <meta itemprop="image" content="/images/geass.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="MingRong's Boat">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">Linux之Shell脚本基础

              
            
          </h1>
        

        <div class="post-meta">

          
          
          

          
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">Posted on</span>
              

              
                
              

              <time title="Created: 2021-02-23 13:35:04 / Modified: 15:03:01" itemprop="dateCreated datePublished" datetime="2021-02-23T13:35:04+08:00">2021-02-23</time>
            </span>
          

          
            

            
          

          
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">In</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing"><a href="/categories/Linux相关/" itemprop="url" rel="index"><span itemprop="name">Linux相关</span></a></span>

                
                
              
            </span>
          

          
            
            
          

          
          

          
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-eye"></i>
                 Views:  
                <span class="busuanzi-value" id="busuanzi_value_page_pv"></span>
              </span>
            </span>
          

          <br>
          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <h1 id="Linux之Shell脚本基础"><a href="#Linux之Shell脚本基础" class="headerlink" title="Linux之Shell脚本基础"></a>Linux之Shell脚本基础</h1><a id="more"></a>

<h2 id="简介"><a href="#简介" class="headerlink" title="简介"></a>简介</h2><p>Shell 是一个用 C 语言编写的程序，它是用户使用 Linux 的桥梁。Shell 既是一种命令语言，又是一种程序设计语言。</p>
<p>Shell 是指一种应用程序，这个应用程序提供了一个界面，用户通过这个界面访问操作系统内核的服务。</p>
<p>Shell脚本（Shell script），是一种为shell编写的脚本程序 ，是一种解释器语言，与JavaScript、python、php类似。</p>
<h2 id="Shell脚本编程"><a href="#Shell脚本编程" class="headerlink" title="Shell脚本编程"></a>Shell脚本编程</h2><h3 id="创建脚本"><a href="#创建脚本" class="headerlink" title="创建脚本"></a>创建脚本</h3><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">vi script.sh</span><br><span class="line"></span><br><span class="line"><span class="meta">#</span> 内容</span><br><span class="line">echo "My first script ..."</span><br><span class="line"></span><br><span class="line"><span class="meta">#</span> 运行脚本</span><br><span class="line">chmod a+x script.sh</span><br><span class="line">./script.sh</span><br></pre></td></tr></table></figure>

<h3 id="基础"><a href="#基础" class="headerlink" title="基础"></a>基础</h3><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#</span> 注释：使用#开头</span><br><span class="line"></span><br><span class="line"><span class="meta">#</span> 变量使用</span><br><span class="line">VAR="test"</span><br><span class="line">echo "$VAR"</span><br><span class="line">echo "$&#123;VAR&#125;"</span><br><span class="line"></span><br><span class="line"><span class="meta">#</span> 可以把命令执行后的输入结果赋值给一个变量</span><br><span class="line">LIST=$(ls)</span><br></pre></td></tr></table></figure>

<h3 id="测试"><a href="#测试" class="headerlink" title="测试"></a>测试</h3><p>测试主要用于条件判断。<code>[ condition-to-test-for ]</code> ，如<code>[ -e /etc/passwd ]</code>，注意的是<code>[]</code><strong>前后必须有空格</strong>，如<code>[-e /etc/passwd]</code>是错误的写法。</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#</span> 文件测试</span><br><span class="line">-d FILE_NAM  # True if FILE_NAM is a directory 是否目录</span><br><span class="line">-e FILE_NAM  # True if FILE_NAM exists 文件是否存在</span><br><span class="line">-f FILE_NAM  # True if FILE_NAM exists and is a regular file 文件是否存在且是普通文件</span><br><span class="line">-r FILE_NAM  # True if FILE_NAM is readable 文件是否可读</span><br><span class="line">-s FILE_NAM  # True if FILE_NAM exists and is not empty 文件是否存在且不为空</span><br><span class="line">-w FILE_NAM  # True if FILE_NAM has write permission 文件是否有可写权限</span><br><span class="line">-x FILE_NAM  # True if FILE_NAM is executable 文件是否可执行</span><br><span class="line"></span><br><span class="line"><span class="meta">#</span> 字符串测试</span><br><span class="line">-z STRING  # True if STRING is empty</span><br><span class="line">-n STRING  # True if STRING is not empty</span><br><span class="line">STRING1 = STRIN2 # True if strings are equal</span><br><span class="line">STRING1 != STRIN2 # True if strings are not equal</span><br><span class="line"></span><br><span class="line"><span class="meta">#</span> 算术测试</span><br><span class="line">var1 -eq var2  # True if var1 is equal to var2 等于</span><br><span class="line">var1 -ne var2  # True if var1 not equal to var2 不等于</span><br><span class="line">var1 -lt var2  # True if var1 is less than var2 小于</span><br><span class="line">var1 -le var2  # True if var1 is less than or equal to var2 小于等于</span><br><span class="line">var1 -gt var2  # True if var1 is greater than var2 大于</span><br><span class="line">var1 -ge var2  # True if var1 is greater than or equal to var2 大于等于</span><br></pre></td></tr></table></figure>

<h3 id="条件判断"><a href="#条件判断" class="headerlink" title="条件判断"></a>条件判断</h3><p>注意：判断语句中[]两边仅左右空格，且中间语句不要有空格。</p>
<h4 id="if-elif-else语句"><a href="#if-elif-else语句" class="headerlink" title="if-elif-else语句"></a>if-elif-else语句</h4><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line">if [ condition-is-true ]</span><br><span class="line">then</span><br><span class="line">  command 1</span><br><span class="line">elif [ condition-is-true ]</span><br><span class="line">then</span><br><span class="line">  command 2</span><br><span class="line">elif [ condition-is-true ]</span><br><span class="line">then</span><br><span class="line">  command 3</span><br><span class="line">else</span><br><span class="line">  command 4</span><br><span class="line">fi</span><br><span class="line"></span><br><span class="line"><span class="meta">#</span> 示例</span><br><span class="line">if [ $1="one" ]</span><br><span class="line">then</span><br><span class="line">        echo "one"</span><br><span class="line">elif [ $1="two" ]</span><br><span class="line">then</span><br><span class="line">        echo "two"</span><br><span class="line">else</span><br><span class="line">        echo "none"</span><br><span class="line">fi</span><br></pre></td></tr></table></figure>

<h4 id="case语句"><a href="#case语句" class="headerlink" title="case语句"></a>case语句</h4><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line">case "$VAR" in</span><br><span class="line">  pattern_1)</span><br><span class="line">    # commands when $VAR matches pattern 1</span><br><span class="line">    ;;</span><br><span class="line">  pattern_2)</span><br><span class="line">    # commands when $VAR matches pattern 2</span><br><span class="line">    ;;</span><br><span class="line">  *)</span><br><span class="line">    # This will run if $VAR doesnt match any of the given patterns</span><br><span class="line">    ;;</span><br><span class="line">esac</span><br><span class="line"></span><br><span class="line"><span class="meta">#</span> 示例</span><br><span class="line">case "$1" in</span><br><span class="line">        [yY] | [yY][eE][sS])</span><br><span class="line">                echo "Yes"</span><br><span class="line">                ;;</span><br><span class="line">        [nN] | [nN][oO])</span><br><span class="line">                echo "No"</span><br><span class="line">                ;;</span><br><span class="line">        *)</span><br><span class="line">                echo "none"</span><br><span class="line">                ;;</span><br><span class="line">esac</span><br></pre></td></tr></table></figure>

<h4 id="for循环"><a href="#for循环" class="headerlink" title="for循环"></a>for循环</h4><p>((表达;条件;末尾循环))，注意是两个括号，否则会报错。</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line">for VARIABLE_NAME in ITEM_1 ITEM_N</span><br><span class="line">do</span><br><span class="line">  command 1</span><br><span class="line">  command 2</span><br><span class="line">    ...</span><br><span class="line">    ...</span><br><span class="line">  command N</span><br><span class="line">done</span><br><span class="line"></span><br><span class="line">for (( VAR=1;VAR&lt;N;VAR++ ))</span><br><span class="line">do</span><br><span class="line">  command 1</span><br><span class="line">  command 2</span><br><span class="line">    ...</span><br><span class="line">    ...</span><br><span class="line">  command N</span><br><span class="line">done</span><br><span class="line"></span><br><span class="line"><span class="meta">#</span> 示例一</span><br><span class="line">COLORS="red green blue"</span><br><span class="line">for COLOR in $COLORS</span><br><span class="line">do</span><br><span class="line">        echo "The color is: $&#123;COLOR&#125;"</span><br><span class="line">done</span><br><span class="line"></span><br><span class="line"><span class="meta">#</span>示例二</span><br><span class="line">for (( VAR=0;VAR&lt;3;VAR++ ))</span><br><span class="line">do</span><br><span class="line">        echo "VAR is : $&#123;VAR&#125;"</span><br><span class="line">done</span><br></pre></td></tr></table></figure>

<h4 id="while循环"><a href="#while循环" class="headerlink" title="while循环"></a>while循环</h4><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line">while [ CONNDITION_IS_TRUE ]</span><br><span class="line">do</span><br><span class="line"><span class="meta">  #</span> Commands will change he entry condition</span><br><span class="line">  command 1</span><br><span class="line">  command 2</span><br><span class="line">    ...</span><br><span class="line">    ...</span><br><span class="line">  command N</span><br><span class="line">done</span><br><span class="line"></span><br><span class="line"><span class="meta">#</span> 示例</span><br><span class="line">VAR_TWO=0</span><br><span class="line">while [ $VAR_TWO -le 3 ]</span><br><span class="line">do</span><br><span class="line">        echo "VAR_TWO is : $&#123;VAR_TWO&#125;"</span><br><span class="line">        ((VAR_TWO++))</span><br><span class="line">done</span><br></pre></td></tr></table></figure>

<h4 id="参数传递"><a href="#参数传递" class="headerlink" title="参数传递"></a>参数传递</h4><p>当我们运行脚本的时候，可以传递参数供脚本内部使用<code>$ ./script.sh param1 param2 param3 param4</code>这些参数将被存储在特殊的变量中</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">$</span>0 -- "script.sh"</span><br><span class="line"><span class="meta">$</span>1 -- "param1"</span><br><span class="line"><span class="meta">$</span>2 -- "param2"</span><br><span class="line"><span class="meta">$</span>3 -- "param3"</span><br><span class="line"><span class="meta">$</span>4 -- "param4"</span><br><span class="line"><span class="meta">$</span># -- 代表参数个数</span><br><span class="line"><span class="meta">$</span>@ -- array of all positional parameters "param1""param2""param3""param4"</span><br><span class="line"><span class="meta">$</span>* -- 代表"param1 param2 param3 param4"，整体中间以空格分开</span><br></pre></td></tr></table></figure>

<h4 id="退出码"><a href="#退出码" class="headerlink" title="退出码"></a>退出码</h4><p>可以自定义</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">exit 0</span><br><span class="line">exit 1</span><br><span class="line">exit 2</span><br><span class="line">  ...</span><br><span class="line">  ...</span><br><span class="line">exit 255</span><br></pre></td></tr></table></figure>

<h3 id="函数"><a href="#函数" class="headerlink" title="函数"></a>函数</h3><p>可以把一些列的命令或语句定义在一个函数内，从程序的其他地方调用。</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><span class="line">function function_name() &#123;</span><br><span class="line">    command 1</span><br><span class="line">    command 2</span><br><span class="line">    command 3</span><br><span class="line">      ...</span><br><span class="line">      ...</span><br><span class="line">    command N</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="meta">#</span> 示例</span><br><span class="line">function myFunc () &#123;</span><br><span class="line">        echo "My function ..."</span><br><span class="line">        if [ $# -eq 1 ]</span><br><span class="line">        then</span><br><span class="line">                echo "My function one"</span><br><span class="line">                return $[$#]</span><br><span class="line">        elif [ $# -eq 2 ]</span><br><span class="line">        then</span><br><span class="line">                echo "My function two"</span><br><span class="line">                return $[$#]</span><br><span class="line">        else</span><br><span class="line">                echo "My function none"</span><br><span class="line">                return $[$#]</span><br><span class="line">        fi</span><br><span class="line">&#125;</span><br><span class="line">myFunc</span><br><span class="line">echo "one is : $?"</span><br><span class="line">myFunc one</span><br><span class="line">echo "two is : $?"</span><br><span class="line">myFunc one two</span><br><span class="line">echo "three is : $?"</span><br></pre></td></tr></table></figure>


      
    </div>

    

    
    
    
    <div>
      
        <div>
    
        <div style="text-align:center;color: #ccc;font-size:14px;">-------------本文结束<i class="fa fa-paw"></i>感谢您的阅读-------------</div>
    
</div>
      
    </div>

    

    
      
    
    

    

    <footer class="post-footer">
      
        
          
        
        <div class="post-tags">
          
            <a href="/tags/Shell脚本/" rel="tag"># Shell脚本</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2021/02/23/Linux相关/Linux之ps命令/" rel="next" title="Linux之ps命令">
                <i class="fa fa-chevron-left"></i> Linux之ps命令
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2021/02/25/软件工程相关/Git使用基础/" rel="prev" title="Git使用基础">
                Git使用基础 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>


  </div>


          </div>
          

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            Table of Contents
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            Overview
          </li>
        </ul>
      

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image" src="/images/geass.jpg" alt="MingRongChen">
            
              <p class="site-author-name" itemprop="name">MingRongChen</p>
              <div class="site-description motion-element" itemprop="description">O Captain! My Captain!</div>
          </div>

          
            <nav class="site-state motion-element">
              
                <div class="site-state-item site-state-posts">
                
                  <a href="/archives/">
                
                    <span class="site-state-item-count">23</span>
                    <span class="site-state-item-name">posts</span>
                  </a>
                </div>
              

              
                
                
                <div class="site-state-item site-state-categories">
                  
                    
                      <a href="/categories/">
                    
                  
                    
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                    <span class="site-state-item-count">9</span>
                    <span class="site-state-item-name">categories</span>
                  </a>
                </div>
              

              
                
                
                <div class="site-state-item site-state-tags">
                  
                    
                      <a href="/tags/">
                    
                  
                    
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                    <span class="site-state-item-count">21</span>
                    <span class="site-state-item-name">tags</span>
                  </a>
                </div>
              
            </nav>
          

          

          

          
            <div class="links-of-author motion-element">
              
                <span class="links-of-author-item">
                  
                  
                    
                  
                  
                    
                  
                  <a href="https://github.com/mingrongchen" title="GitHub &rarr; https://github.com/mingrongchen" rel="noopener" target="_blank"><i class="fa fa-fw fa-github"></i>GitHub</a>
                </span>
              
                <span class="links-of-author-item">
                  
                  
                    
                  
                  
                    
                  
                  <a href="mailto:825296313@qq.com" title="E-Mail &rarr; mailto:825296313@qq.com" rel="noopener" target="_blank"><i class="fa fa-fw fa-envelope"></i>E-Mail</a>
                </span>
              
            </div>
          

          

          
          

          
            
          
          

        </div>
      </div>

      
      <!--noindex-->
        <div class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
            
            
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#Linux之Shell脚本基础"><span class="nav-number">1.</span> <span class="nav-text">Linux之Shell脚本基础</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#简介"><span class="nav-number">1.1.</span> <span class="nav-text">简介</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Shell脚本编程"><span class="nav-number">1.2.</span> <span class="nav-text">Shell脚本编程</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#创建脚本"><span class="nav-number">1.2.1.</span> <span class="nav-text">创建脚本</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#基础"><span class="nav-number">1.2.2.</span> <span class="nav-text">基础</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#测试"><span class="nav-number">1.2.3.</span> <span class="nav-text">测试</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#条件判断"><span class="nav-number">1.2.4.</span> <span class="nav-text">条件判断</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#if-elif-else语句"><span class="nav-number">1.2.4.1.</span> <span class="nav-text">if-elif-else语句</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#case语句"><span class="nav-number">1.2.4.2.</span> <span class="nav-text">case语句</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#for循环"><span class="nav-number">1.2.4.3.</span> <span class="nav-text">for循环</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#while循环"><span class="nav-number">1.2.4.4.</span> <span class="nav-text">while循环</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#参数传递"><span class="nav-number">1.2.4.5.</span> <span class="nav-text">参数传递</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#退出码"><span class="nav-number">1.2.4.6.</span> <span class="nav-text">退出码</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#函数"><span class="nav-number">1.2.5.</span> <span class="nav-text">函数</span></a></li></ol></li></ol></li></ol></div>
            

          </div>
        </div>
      <!--/noindex-->
      

      

    </div>
  </aside>
  


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; 2019 – <span itemprop="copyrightYear">2022</span>
  <span class="with-love" id="animate">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">MingRong</span>

  

  
</div>









        
<div class="busuanzi-count">
  <script async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>

  
    <span class="post-meta-item-icon">
      <i class="fa fa-user"></i>
    </span>
    <span class="site-uv" title="Total Visitors">
      <span class="busuanzi-value" id="busuanzi_value_site_uv"></span>
    </span>
  

  
    <span class="post-meta-divider">|</span>
  

  
    <span class="post-meta-item-icon">
      <i class="fa fa-eye"></i>
    </span>
    <span class="site-pv" title="Total Views">
      <span class="busuanzi-value" id="busuanzi_value_site_pv"></span>
    </span>
  
</div>









        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

    

    
  </div>

  

<script>
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>


























  
  <script src="/lib/jquery/index.js?v=3.4.1"></script>

  
  <script src="/lib/velocity/velocity.min.js?v=1.2.1"></script>

  
  <script src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>


  


  <script src="/js/utils.js?v=7.2.0"></script>

  <script src="/js/motion.js?v=7.2.0"></script>



  
  


  <script src="/js/affix.js?v=7.2.0"></script>

  <script src="/js/schemes/pisces.js?v=7.2.0"></script>



  
  <script src="/js/scrollspy.js?v=7.2.0"></script>
<script src="/js/post-details.js?v=7.2.0"></script>



  


  <script src="/js/next-boot.js?v=7.2.0"></script>


  

  

  

  


  


  




  

  

  

  

  

  

  

  

  

  

  

  

  

  

<script src="/live2dw/lib/L2Dwidget.min.js?094cbace49a39548bed64abff5988b05"></script><script>L2Dwidget.init({"pluginRootPath":"live2dw/","pluginJsPath":"lib/","pluginModelPath":"assets/","tagMode":false,"log":false,"model":{"scale":1.5,"hHeadPos":0.5,"vHeadPos":0.618,"jsonPath":"/live2dw/assets/z16.model.json"},"display":{"superSample":2,"position":"left","width":150,"height":300,"hOffset":0,"vOffset":-20},"mobile":{"show":false,"scale":0.5,"hOffset":0,"vOffset":-10},"react":{"opacity":0.8,"opacityDefault":0.7,"opacityOnHover":0.2}});</script></body>
</html>
